import { store } from '../index'
import { fetchDictionaryData } from '@/api'
interface DictState {
  dict: Record<string, any>
}
export const useDictStore = defineStore('dict', {
  state: (): DictState => {
    return {
      dict: [] as any[]
    }
  },
  getters: {
    getDict(): object {
      return this.dict
    }
  },
  actions: {
    // 设置字典
    setDict(data: object) {
      this.dict = data
    },
    getDictItem(key: string, code: string) {
      return this.dict.find(
        (item: { siteCode: string; dictionaryCode: string; dictionaryName: string }) =>
          item.siteCode === key && (item.dictionaryCode == code || item.dictionaryName == code)
      )
    },
    // // 设置字典
    // setDictKey(key: string, data: any) {
    //   this.dict[key] = data
    // },
    // // 删除字典
    // removeDict(key: string) {
    //   delete this.dict[key]
    // },
    // 清空字典
    cleanDict() {
      this.dict = []
    },
    async fetchDictionary() {
      try {
        const { data } = await fetchDictionaryData()
        this.dict = data
      } catch (error) {}
    }
  },
  persist: {
    storage: sessionStorage
  }
})

export const useDictStoreWithOut = () => {
  return useDictStore(store)
}
